package com.jd.dlc.aspect;

import com.jd.dlc.el.ExpressionEvaluator;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.expression.EvaluationContext;

import java.lang.reflect.Method;

/**
 * @program: dlc
 * @ClassName BAspect
 * @description:
 * @author: sizegang
 * @create: 2022-05-10
 **/
public abstract class BAspect {
    private static final ExpressionEvaluator<String> EVALUATOR = new ExpressionEvaluator<>();

    /**
     * Analytical expression
     * @param point
     * @param param
     * @return
     */
    public String evalEL(ProceedingJoinPoint point, String param) {
        MethodSignature ms = (MethodSignature) point.getSignature();
        Method method = ms.getMethod();
        Object[] args = point.getArgs();
        Object target = point.getTarget();
        Class<?> targetClass = target.getClass();
        EvaluationContext context = EVALUATOR.createEvaluationContext(target, target.getClass(), method, args);
        AnnotatedElementKey elementKey = new AnnotatedElementKey(method, targetClass);
        return EVALUATOR.condition(param, elementKey, context, String.class);
    }
}
